Imports System.Data

Partial Class WebControls_POlicySearch
    Inherits System.Web.UI.UserControl

    Dim strPolicyCd As String = ""
    Dim strPolicyNo As String = ""
    Dim strClientName As String = ""
    Dim strProvider As String = ""
    Dim strProduct As String = ""
    Dim dAmount As Double
    Dim strCurrency As String

    'TODO: Add properties client name, provider, product, policy no, amount

    Public Event Policy_Selected()

#Region "Control Properties"

    Public Property Policy_cd() As String
        Get
            Policy_cd = strPolicyCd
        End Get
        Set(ByVal value As String)
            strPolicyCd = value
        End Set
    End Property

    Public Property PolicyNo() As String
        Get
            PolicyNo = strPolicyNo
        End Get
        Set(ByVal value As String)
            strPolicyNo = value
        End Set
    End Property

    Public Property ClientName() As String
        Get
            ClientName = strClientName
        End Get
        Set(ByVal value As String)
            strClientName = value
        End Set
    End Property

    Public Property Provider() As String
        Get
            Provider = strProvider
        End Get
        Set(ByVal value As String)
            strProvider = value
        End Set
    End Property

    Public Property Product() As String
        Get
            Product = strProduct
        End Get
        Set(ByVal value As String)
            strProduct = value
        End Set
    End Property

    Public Property ContributionAmount() As Double
        Get
            ContributionAmount = dAmount
        End Get
        Set(ByVal value As Double)
            dAmount = value
        End Set
    End Property

    Public Property Currency_Cd() As String
        Get
            Currency_Cd = strCurrency
        End Get
        Set(ByVal value As String)
            strCurrency = value
        End Set
    End Property

#End Region

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        If e.CommandName = "Select" Then
            strPolicyCd = e.Item.Cells(1).Text
            strClientName = e.Item.Cells(2).Text
            strPolicyNo = e.Item.Cells(3).Text
            strProvider = e.Item.Cells(4).Text
            strProduct = e.Item.Cells(5).Text
            dAmount = Val(e.Item.Cells(6).Text)
            strCurrency = e.Item.Cells(7).Text

            If strPolicyCd = "&nbsp;" Then strPolicyCd = ""
            If strClientName = "&nbsp;" Then strClientName = ""
            If strPolicyNo = "&nbsp;" Then strPolicyNo = ""
            If strProvider = "&nbsp;" Then strProvider = ""
            If strProduct = "&nbsp;" Then strProduct = ""
            If strCurrency = "&nbsp;" Then strCurrency = ""

            RaiseEvent Policy_Selected()
        End If
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        If Not IsNothing(ViewState("dtable")) Then
            If DataGrid.PageCount >= e.NewPageIndex Then
                DataGrid.CurrentPageIndex = e.NewPageIndex
                DataGrid.DataSource = ViewState("dtable")
                DataGrid.DataBind()
            End If
        End If
    End Sub

    Public Sub Search()
        Dim dm As New DAOMain.VW_POLICY_TRANS_SEARCH_DAO
        Dim vo As New DAOMain.VW_POLICY_TRANS_SEARCH_VO

        With vo
            .POLICY_CLIENT_LAST_NAME = "%" & Me.Lastname_txt.Text & "%"
            .POLICY_CLIENT_FIRST_NAME = "%" & Me.Firstname_txt.Text & "%"
            .POLICY_CLIENT_MIDDLE_NAME = "%" & Me.Middlename_txt.Text & "%"
            .POLICY_CLIENT_ALIAS = "%" & Me.Alias_txt.Text & "%"
            .POLICY_NO = "%" & Me.Policynumber_txt.Text & "%"
            .PROVIDER_DESC = "%" & Me.Provider_txt.Text & "%"
            .PRDT_DESC = "%" & Me.Product_txt.Text & "%"

        End With

        Dim dt As DataTable = dm.Search(vo, , True)

        results_label.Text = dt.Rows.Count

        ViewState.Add("dtable", dt)
        DataGrid.CurrentPageIndex = 0
        DataGrid.DataSource = ViewState("dtable")
        DataGrid.DataBind()

        dm = Nothing
    End Sub

    Protected Sub Search_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_btn.Click
        Call Search()
    End Sub
End Class
